package com.css.fxfzfxqh.modules.beyondProbability.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzfxqh.common.rest.JcsjRestManager;
import com.css.fxfzfxqh.common.rest.model.bo.JcsjDataViewBo;
import com.css.fxfzfxqh.common.rest.model.bo.JcsjVersionPageBo;
import com.css.fxfzfxqh.common.rest.model.param.JcsjDataViewParam;
import com.css.fxfzfxqh.common.rest.model.param.JcsjVersionPageParam;
import com.css.fxfzfxqh.common.rest.model.vo.JcsjVersionPageVo;
import com.css.fxfzfxqh.constants.FxfzConstants;
import com.css.fxfzfxqh.modules.beyondProbability.dto.BeyondProVersionPageParam;
import com.css.fxfzfxqh.modules.beyondProbability.service.BeyondProbabilityService;
import com.css.fxfzfxqh.modules.beyondProbability.vo.ParamsVo;
import com.css.fxfzfxqh.modules.buildingKilometerGrid.exceptions.BizException;
import com.css.fxfzfxqh.util.RestTemplateUtil;
import com.css.fxfzfxqh.zcpt.sys.service.SysAreaService;
import com.css.fxfzfxqh.zcpt.sys.service.impl.SysAreaCacheService;
import org.slw.common.helper.PropsHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Service
public class BeyondProbabilityServiceImpl implements BeyondProbabilityService {

    @Autowired
    RestTemplateUtil restTemplateUtil;
    @Autowired
    JcsjRestManager jcsjRestManager;
    @Autowired
    private SysAreaCacheService sysAreaCacheService;
    @javax.annotation.Resource
    RedisTemplate redisTemplate;
    @Autowired
    SysAreaService sysAreaService;

    @Override
    public JSONObject page(ParamsVo paramsVo, int curPage, int pageSize) {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("jcsjRestUrl") + "/jcLd/fxqh/page?dataLevel=" + paramsVo.getDataLevel() + "&version=" + paramsVo.getVersion() + "&province=" + paramsVo.getProvince() + "&city=" + paramsVo.getCity() + "&county=" + paramsVo.getCounty() + "&curPage=" + curPage + "&pageSize=" + pageSize;
        if (null != paramsVo.getNum()) {
            if (paramsVo.getNum().equals("1")) {
                url = configGroup.getProperty("jcsjRestUrl") + "/jcPga/fxqh/page?dataLevel=" + paramsVo.getDataLevel() + "&version=" + paramsVo.getVersion() + "&province=" + paramsVo.getProvince() + "&city=" + paramsVo.getCity() + "&county=" + paramsVo.getCounty() + "&curPage=" + curPage + "&pageSize=" + pageSize;
            }
        }
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("dataLevel", paramsVo.getDataLevel());
        paramMap.put("version", paramsVo.getVersion());
        paramMap.put("province", paramsVo.getProvince());
        paramMap.put("city", paramsVo.getCity());
        paramMap.put("county", paramsVo.getCounty());
        paramMap.put("curPage", curPage);
        paramMap.put("pageSize", pageSize);
        ResponseEntity<String> responseEntity = restTemplateUtil.sendHttpRequest(url, paramMap);
        String body = responseEntity.getBody();
        JSONObject jsonObject = JSONObject.parseObject(body);
        return jsonObject;
    }

    @Override
    public JSONObject getVersion(String num) {
        String param = "jcld";
        if (null != num) {
            if (num.equals("1")) {
                param = "pga";
            }
        }
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("datacenterRestUrl") + "/sysDataVersion/fxqh/versionList/" + param;
        Map<String, Object> paramMap = new HashMap<>();
        ResponseEntity<String> response = restTemplateUtil.sendHttpRequest(url, paramMap);
        String versionBody = response.getBody();
        JSONObject jsonObject = JSONObject.parseObject(versionBody);
        return jsonObject;
    }

    @Override
    public JcsjVersionPageBo versionPage(BeyondProVersionPageParam param) throws BizException {
        JcsjVersionPageParam jcsjVersionPageParam = new JcsjVersionPageParam();
        BeanUtils.copyProperties(param, jcsjVersionPageParam);
        if (param.getTypeCode() == null) {
            param.setTypeCode("jcld");
        }
        jcsjVersionPageParam.setTypeLevel3Code(param.getTypeCode());
        JcsjVersionPageBo r = jcsjRestManager.versionPage(jcsjVersionPageParam);
        if (r.getCode() == 200) {
            Map<String, String> areaIdCodeMap = (Map<String, String>) JSON.parse((String) redisTemplate.opsForValue().get(FxfzConstants.CACHE_SYS_KEY + "areaIdCodeMap"));
            for (JcsjVersionPageVo jcsjVersionPageVo : r.getData().getList()) {
                jcsjVersionPageVo.setProvinceName(sysAreaCacheService.getAreaNameByAreaId(jcsjVersionPageVo.getDataRangeProvince()));
                jcsjVersionPageVo.setCityName(sysAreaCacheService.getAreaNameByAreaId(jcsjVersionPageVo.getDataRangeCity()));
                jcsjVersionPageVo.setAreaName(sysAreaCacheService.getAreaNameByAreaId(jcsjVersionPageVo.getDataRangeArea()));
                // String code = null;
                // if (areaIdCodeMap==null||areaIdCodeMap.size()<1) {
                //     if ("1".equals(jcsjVersionPageVo.getDataRangeCountry()+"")){
                //         code = sysAreaService.getDivisionCodeById(jcsjVersionPageVo.getDataRangeProvince());
                //     }else if ("2".equals(jcsjVersionPageVo.getDataRangeCountry()+"")){
                //         code = sysAreaService.getDivisionCodeById(jcsjVersionPageVo.getDataRangeCity());
                //     }else {
                //         code = sysAreaService.getDivisionCodeById(jcsjVersionPageVo.getDataRangeArea());
                //     }
                // }else {
                //     if ("1".equals(jcsjVersionPageVo.getDataRangeCountry()+"")){
                //         code = areaIdCodeMap.get(jcsjVersionPageVo.getDataRangeProvince());
                //     }else if ("2".equals(jcsjVersionPageVo.getDataRangeCountry()+"")){
                //         code = areaIdCodeMap.get(jcsjVersionPageVo.getDataRangeCity());
                //     }else {
                //         code = areaIdCodeMap.get(jcsjVersionPageVo.getDataRangeArea());
                //     }
                // }
                // String geom = jcsjRestManager.getGeomByCode(code,jcsjVersionPageVo.getDataRangeCountry()+"");
                // jcsjVersionPageVo.setGeomText(geom);
            }
        }
        return r;
    }

    @Override
    public JcsjDataViewBo queryDataView(JcsjDataViewParam param) throws BizException {
        return jcsjRestManager.queryDataView(param);
    }


}
